package lg.minecraft.plugin.worldprotect.gui;

import lg.minecraft.plugin.library.inventory.CancelInventoryContext;
import lg.minecraft.plugin.library.inventory.ClickInventoryContext;
import lg.minecraft.plugin.library.inventory.InventoryContext;
import lg.minecraft.plugin.library.inventory.LGInventory;
import lg.minecraft.plugin.worldprotect.LGWorldProtect;
import lg.minecraft.plugin.worldprotect.WorldSettingType;
import lg.minecraft.plugin.worldprotect.config.Language;
import lg.minecraft.plugin.worldprotect.util.WorldSettings;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author langle__
 * @version 1.0
 */
public class SettingsGUI extends LGInventory {

    private static final int[] GLASS_ARRAY = {45, 46, 47, 48, 50, 51, 52, 53};

    private final WorldSettings worldSettings;

    public SettingsGUI(World world) {
        super(6, "§f设置 §7| §f" + world.getName());
        this.worldSettings = LGWorldProtect.getWorldSettings(world.getName());
    }

    @Override
    public void onInventoryOpen(CancelInventoryContext context) {
        Language language = LGWorldProtect.getLanguage();
        int index = 0;
        int size = inventory.getSize();
        for (WorldSettingType value : WorldSettingType.values()) {
            Material icon = LGWorldProtect.getIcon(value);
            ItemStack item = new ItemStack(icon);
            ItemMeta meta = item.getItemMeta();
            if (meta == null) continue;
            meta.setDisplayName(language.getWorldSettingName(value));
            List<String> lore = new ArrayList<>();
            boolean enabled = worldSettings.isEnabled(value);
            lore.add("§f状态: " + (enabled ? language.getEnable() : language.getDisable()));
            lore.add("");
            String[] array = language.getWorldSettingDescription(value).split("\n");
            Collections.addAll(lore, array);
            lore.add("");
            lore.add("§f点击切换");
            meta.setLore(lore);
            if (enabled) {
                meta.addEnchant(Enchantment.BINDING_CURSE, 1, true);
                meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
            }
            item.setItemMeta(meta);
            inventory.setItem(index++, item);
            if (index >= size) break;
        }
        ItemStack borderItem = new ItemStack(Material.GRAY_STAINED_GLASS_PANE);
        ItemMeta borderMeta = borderItem.getItemMeta();
        if (borderMeta != null) {
            borderMeta.setDisplayName("§r");
            borderItem.setItemMeta(borderMeta);
        }
        for (int slot : GLASS_ARRAY) inventory.setItem(slot, borderItem);
        ItemStack backItem = new ItemStack(Material.RED_STAINED_GLASS_PANE);
        ItemMeta backMeta = backItem.getItemMeta();
        if (backMeta != null) {
            backMeta.setDisplayName("§c返回");
            backItem.setItemMeta(backMeta);
        }
        inventory.setItem(49, backItem);
    }

    @Override
    public void onInventoryClick(ClickInventoryContext context) {
        context.setCancelled(true);
        Player player = context.getPlayer();
        int slot = context.getSlot();
        WorldSettingType[] values = WorldSettingType.values();
        if (slot < values.length) {
            WorldSettingType type = values[slot];
            if (worldSettings.isEnabled(type)) {
                worldSettings.disable(type);
                player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1f, 1f);
            } else {
                worldSettings.enable(type);
                player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_YES, 1f, 1f);
            }
            onInventoryOpen(context);
            LGWorldProtect.getWorlds().save();
        } else if (slot == 49) {
            new MainGUI().openInventory(player);
            player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
        }
    }

    @Override
    public void onInventoryClose(InventoryContext context) {
    }

}
